package com.mysiteforme.admin.base.control;

import com.alibaba.fastjson.JSONObject;
import com.mysiteforme.admin.base.entity.User;
import com.mysiteforme.admin.base.param.ResponeResult;
import com.mysiteforme.admin.base.service.LoginService;
import com.mysiteforme.admin.base.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by heqichao on 2018-2-12.
 */
//@RestController == @Controller + @ResponseBody
@RestController
@RequestMapping(value = "/service")
public class LoginController extends BaseController{

    @Autowired
    private LoginService loginSerice;


    @RequestMapping(value = "/login" )
    @ResponseBody
    public ResponeResult login(@RequestBody Map map) throws Exception {
//        String userNo = (String) map.get("userNo");
//        System.out.println(userNo);
        ResponeResult responeResult=loginSerice.login(map.get("username").toString().trim(),map.get("password").toString());
        User user = ServletUtil.getSessionUser();
        return responeResult;
    }

    @RequestMapping(value = "/info", method = {RequestMethod.POST,RequestMethod.GET} )
    @ResponseBody
    public ResponeResult info(@RequestBody Map map)  {
//        String userNo = (String) map.get("userNo");
//        System.out.println(userNo);
//        JSONObject object = JSONObject.parseObject("{\n" +
//                "        \"roles\":[\n" +
//                "            \"admin\"\n" +
//                "        ],\n" +
//                "        \"name\":\"admin\",\n" +
//                "        \"avatar\":\"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif\"\n" +
//                "    } ");


        // TODO: 2019-02-28 查找用户的信息 组装用户的权限数据
        Map data = new HashMap();
        User user = ServletUtil.getSessionUser();
        if(user!=null){
            Set roles = new HashSet();
            roles.add("admin");
            data.put("roles",roles.toArray());
            data.put("name",user.getAccount());
            data.put("avatar",user.getRemark());
            ResponeResult responeResult =new ResponeResult(data,20000);
            return responeResult;
        }else{
            return new ResponeResult(true,"没有找到对应的用户","errorMsg",50008);
        }

    }

    @RequestMapping(value = "/logout")
    @ResponseBody
    public ResponeResult logout(){
        ServletUtil.setSessionUser(null);
        return   ServletUtil.NO_LOGIN_RESULT;
    }


    @RequestMapping(value = "/checkLogin" )
    @ResponseBody
    public ResponeResult checkLogin() throws Exception {
       /* if(ServletUtil.getSessionUser()==null){
            return new ResponeResult(false);
        }*/
//        return new ResponeResult(ServletUtil.getSessionUser());


        Map data = new HashMap();
        User user = ServletUtil.getSessionUser();
        if(user!=null){
            Set roles = new HashSet();
            roles.add("admin");
            data.put("roles",roles.toArray());
            data.put("name",user.getAccount());
            data.put("avatar",user.getRemark());
            ResponeResult responeResult =new ResponeResult(data,20000);
            return responeResult;
        }else{
            return new ResponeResult(true,"没有找到对应的用户","errorMsg",50008);
        }
    }



}
